package ClientCommands;

import java.io.IOException;

import org.json.simple.JSONObject;

import Annotations.IsClientCommand;
import Commands.ClientCommandBase;
import Commands.ExecuteMethod;
import Helpers.ApplicationLogger;
import ServiceLayer.ServiceFacade;

@IsClientCommand
public class AddMessage extends ClientCommandBase<Boolean> 
{
	private String message;
	private String userIdFrom;
	private String userIdTo;
	private boolean isPrivate;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUserIdFrom() {
		return userIdFrom;
	}

	public void setUserIdFrom(String userIdFrom) {
		this.userIdFrom = userIdFrom;
	}

	public String getUserIdTo() {
		return userIdTo;
	}

	public void setUserIdTo(String userIdTo) {
		this.userIdTo = userIdTo;
	}

	public boolean isPrivate() {
		return isPrivate;
	}

	public void setPrivate(boolean isPrivate) {
		this.isPrivate = isPrivate;
	}
	
	public AddMessage()
	{
		setCommandName("AddMessage");
		setCommandDescription("Add message command");
		setExecuteMethod(ExecuteMethod.ServerOnly);
		setPrivate(false);
	}
	
	@Override
	protected void ExecuteInternal() throws IOException {
		boolean status = false;
		try {
			ServiceLayer.ServiceFacade sf = new ServiceFacade();
			status = sf.AddMessage(getMessage(), getUserIdFrom(), getUserIdTo(), isPrivate());
		} catch (Throwable e) {
			ApplicationLogger.getInstance().LOG.error(e.getMessage());
		}
        setResult(status);	
	}
	
	@Override
	public void FillFromJSON(JSONObject jObj) {
		super.FillFromJSON(jObj);
		setMessage((String)jObj.get("message"));
		setUserIdFrom((String)jObj.get("userIdFrom"));
		setUserIdTo((String)jObj.get("userIdTo"));
		setPrivate(Boolean.valueOf(jObj.get("isPrivate").toString()));
	}
	
	@Override
	public String toJSONString() {
		StringBuffer sb = new StringBuffer();                                  
		sb.append("{");                        
		sb.append("\"" + JSONObject.escape("CommandName") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + JSONObject.escape(getCommandName()) + "\"");                                  
		sb.append(",");                                  
		sb.append("\"" + JSONObject.escape("CommandDescription") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + getCommandDescription() + "\"");   
		sb.append(",");                                  
		sb.append("\"" + JSONObject.escape("ExecuteMethod") + "\"");    
		sb.append(":");
		sb.append("\"" + getExecuteMethod() + "\"");   
		sb.append(",");
		sb.append("\"" + JSONObject.escape("Message") + "\"");
		sb.append(":");
		sb.append("\"" + getMessage() + "\"");
		sb.append(",");
		sb.append("\"" + JSONObject.escape("UserIdTo") + "\"");
		sb.append(":");
		sb.append("\"" + getUserIdTo() + "\""); 
		sb.append(",");
		sb.append("\"" + JSONObject.escape("isPrivate") + "\"");
		sb.append(":");
		sb.append("\"" + isPrivate() + "\""); 
		sb.append("}");                                  
		return sb.toString();
	}

}
